package com.itheima01;

import org.w3c.dom.ls.LSOutput;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test02 {


    public static void main(String[] args) throws IOException {
        String path2 = "Test\\dir\\subways.txt";


        BufferedReader br1 = new BufferedReader(new FileReader(path2));
        ArrayList<Subway> arr1 = new ArrayList<>();
        String line1;
        while ((line1 = br1.readLine()) != null) {
            String[] split = line1.split(",");
            Subway subway = new Subway();
            subway.setSubwayId(split[0]);
            subway.setSubwayName(split[1]);
            subway.setWay(split[2]);
            arr1.add(subway);
        }
        br1.close();
        arr1.forEach(s-> System.out.println(s));
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作:1,查看地铁站,2,添加地铁站");
        String s = sc.nextLine();


//        BufferedReader br1 = new BufferedReader(new FileReader(path2));
//        ArrayList<Subway> arr1 = new ArrayList<>();
//        String line1;
//        while ((line1 = br1.readLine()) != null) {
//            String[] split = line1.split(",");
//            Subway subway = new Subway();
//            subway.setSubwayId(split[0]);
//            subway.setSubwayName(split[1]);
//            subway.setWay(split[2]);
//            arr1.add(subway);
//        }
//        br1.close();


        switch (s) {
            case "1":
               if(arr1.isEmpty()){
                   System.out.println("地铁无信息");
               }else {
                   arr1.forEach(a-> System.out.println(a));
               }

                break;
            case "2":
                BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
                System.out.println("请输入地铁站编号,名称,所属路线:");
                String s1 = sc.nextLine();
                String s2 = sc.nextLine();
                String s3 = sc.nextLine();
                Subway subway1 = new Subway();
                subway1.setSubwayId(s1);
                subway1.setSubwayName(s2);
                subway1.setWay(s3);
                arr1.add(subway1);
                for (Subway subway : arr1) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(subway.getSubwayId()).append(",");
                    sb.append(subway.getSubwayName()).append(",");
                    sb.append(subway.getWay());
                    bw.write(sb.toString());
                    bw.newLine();
                    bw.flush();
                }
                bw.close();
                break;
        }
    }

//    public static void method2() {
//    }
//
//    public static void method1() {
//
//    }
}
